package com.ymttest.utils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ymt.core.tool.Logger;


public class YMTDateUtil {

	public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
	public static final String YMDHMSNOSPACE = "yyyyMMddHHmmss";
	public static final String YMD = "yyyy-MM-dd";
	public static final String YMDHMSS = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String YMDTHMS_STRING = "yyyy-MM-dd'T'HH:mm:ss";
	public static final String YMDTHMSS_STRING = "yyyy-MM-dd'T'HH:mm:ss.SSS";
	public static final String YMDGMT="EEE MMM dd HH:mm:ss z yyyy";
	/**
	 * 获取LocalDateTime
	 * @param date 时间字符串
	 * @param format 时间格式
	 * @return
	 */
	public static LocalDateTime getLocalDateTime(String date,String format) {
		 DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
		 LocalDateTime ldt = LocalDateTime.parse(date,df);
		 return ldt;
	}
	public static SimpleDateFormat getYMDHMSFormat() {
		SimpleDateFormat sdf = new SimpleDateFormat(YMDHMS);
		return sdf;
	}

	public static SimpleDateFormat getYMDFormat() {
		SimpleDateFormat sdf = new SimpleDateFormat(YMD);
		return sdf;
	}

	/***
	 * mongoDB ISODate 格式转换为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param mongoTime
	 * @return
	 */
	public static String getFromISODate(String mongoTime) {
		Matcher matcher = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z").matcher(mongoTime);
		return matcher.find() ? matcher.group(0).replaceFirst("T", " ").replaceFirst(".\\d{3}Z", "") : null;
	}
	/**
	 * 转换类似 Sat May 27 10:52:49 CST 2017
	 * @param date
	 * @param format 空时yyyy-MM-dd HH:mm:ss
	 * @return
	 * @throws ParseException
	 */
	public static String getFromDTSDate(String date,String format) throws ParseException{
//		Logger.debug(date);
		SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
		String formatDate="";
		if(format==null||format.length()==0){
			formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sdf.parse(date));  
		}else{
			formatDate = new SimpleDateFormat(format).format(sdf.parse(date));  
		}
		return formatDate;
	}
	/**
	 * 字符串转Date<br>
	 * 格式: "yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseYMDHMSDate(String dateStr) {
		return parseDate(dateStr, YMDHMS);
	}


	/**
	 * 字符串转Date<br>
	 * 格式: "yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate(String dateStr, String format) {
		Date date = null;
		if (dateStr != null && "".equals(dateStr.trim()) == false) {
			try {
				date = new SimpleDateFormat(format).parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}

	/**
	 * 字符串转Date<br>
	 * 格式: "yyyy-MM-dd"
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseYMDDate(String dateStr) {
		return parseDate(dateStr, YMD);
	}

	/**
	 * 获取当前时间 格式 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return 获取当前时间
	 */
	public static String getSysTime() {
		Calendar calendar = Calendar.getInstance();
		String strSysTime = new SimpleDateFormat(YMDHMS).format(calendar.getTime());
		return strSysTime;
	}

	/**
	 * 获取当前时间 格式 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return 获取当前时间
	 */
	public static String getSysTime(String format) {
		Calendar calendar = Calendar.getInstance();
		String strSysTime = new SimpleDateFormat(format).format(calendar.getTime());
		return strSysTime;
	}

	/**
	 * 获取当前时间
	 * 
	 * @return date
	 */
	public static Date getDate() {
		Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	/**
	 * Calendar对象转为String
	 * 
	 * @param c
	 * @param format
	 *            日期格式,例如: yyyy-MM-dd
	 * @return
	 */
	public static String calendarToString(Calendar c, String format) {
		return new SimpleDateFormat(format).format(c.getTime());
	}

	/**
	 * 获取格式化时间 格式 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 *            日期对象
	 * @param format
	 *            格式串,如: yyyy-MM-dd HH:mm:ss
	 * @return 格式化后的日期string
	 */
	public static String getFormatTime(Date date, String format) {
		String strSysTime = new SimpleDateFormat(format).format(date);
		return strSysTime;
	}

	/**
	 * 获取格式化时间 格式 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 *            日期对象, 可以是Date,Timestamp等对象
	 * @param format
	 *            格式 yyyy-MM-dd HH:mm:ss
	 * @return 格式化后的日期string
	 */
	public static String getObjectFormatTime(Object date, String format) {
		String strSysTime = new SimpleDateFormat(format).format(date);
		return strSysTime;
	}

	/**
	 * 获取和当前时间差n天的时间, 格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return now+n天的时间（n=1为明天，n=-1为昨天）
	 */
	public static String getBeforeOrNextDay(int n) {
		return addDays(n, YMDHMS);
	}

	private static String addDays(Calendar calendar, int n, String format) {
		calendar.add(Calendar.DAY_OF_MONTH, n);
		return new SimpleDateFormat(format).format(calendar.getTime());
	}

	public static String addDays(int n, String format) {
		return addDays(Calendar.getInstance(), n, format);
	}

	/**
	 * 获取和指定时间差n天的时间
	 * 
	 * @param dateStr
	 * @param n
	 * @param format
	 *            时间格式
	 * @return
	 */
	public static String getBeforeOrNextDay(String dateStr, int n, String format) {
		if (DataUtil.Stringg.isNullOrEmpty(dateStr)) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDate(dateStr, format));
		return addDays(calendar, n, format);
	}

	public static String getBeforeOrNextMinutes(String dateStr, int n, String format) {
		if (DataUtil.Stringg.isNullOrEmpty(dateStr)) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDate(dateStr, format));
		return addMinutes(calendar, n, format);
	}

	
	public static String getDateBeforeOrNextSecond(String dateStr, int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDate(dateStr, YMDHMS));
		calendar.add(Calendar.SECOND, n);
		return new SimpleDateFormat(YMDHMS).format(calendar.getTime());
		
	}
	
	/**
	 * 获取和指定时间差n天的时间, 格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return dateStr+n天的时间
	 */
	public static String getBeforeOrNextDay(String dateStr, int n) {
		return getBeforeOrNextDay(dateStr, n, YMDHMS);
	}

	private static String addMonths(Calendar calendar, int n, String format) {
		calendar.add(Calendar.MONTH, n);
		return new SimpleDateFormat(format).format(calendar.getTime());
	}

	private static String addMinutes(Calendar calendar, int n, String format) {
		calendar.add(Calendar.MINUTE, n);
		return new SimpleDateFormat(format).format(calendar.getTime());
	}

	public static String addMonths(int n, String format) {
		return addMonths(Calendar.getInstance(), n, format);
	}

	public static String getBeforeOrNextMonth(String dateStr, int n, String format) {
		if (DataUtil.Stringg.isNullOrEmpty(dateStr)) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDate(dateStr, format));
		return addMonths(calendar, n, format);
	}

	/**
	 * 获取和指定时间差n天的时间, 格式yyyy-MM-dd
	 * 
	 * @param n
	 * @return dateStr+n天的时间
	 */
	public static String getBeforeOrNextDayStr(String dateStr, int n) {
		return getBeforeOrNextDay(dateStr, n, YMD);
	}

	/**
	 * 获取和当前时间差n天的时间, 格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return now+n天的时间（n=1为下个月，n=-1为上个月）
	 */
	public static String getBeforeOrNextMonth(int n) {
		return addMonths(n, YMDHMS);
	}

	/**
	 * 获取和指定时间差n天的时间, 格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return dateStr+n个月的时间
	 */
	public static String getBeforeOrNextMonth(String dateStr, int n) {
		return getBeforeOrNextMonth(dateStr, n, YMDHMS);
	}

	public static String getBeforeOrNextMonth(int n, String format) {
		return addMonths(n, format);
	}

	private static String addYears(Calendar calendar, int n, String format) {
		calendar.add(Calendar.YEAR, n);
		return new SimpleDateFormat(format).format(calendar.getTime());
	}

	private static String addHours(Calendar calendar, int n, String format) {
		calendar.add(Calendar.HOUR, n);
		return new SimpleDateFormat(format).format(calendar.getTime());
	}

	public static String addYears(int n, String format) {
		return addYears(Calendar.getInstance(), n, format);
	}

	/**
	 * 获取和当前时间差n天的时间, 格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return now+n年的时间（n=1为明年，n=-1为去年）
	 */
	public static String getBeforeOrNextYear(int n) {
		return addYears(n, YMDHMS);
	}

	public static String getBeforeOrNextYear(String dateStr, int n, String format) {
		if (DataUtil.Stringg.isNullOrEmpty(dateStr)) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDate(dateStr, format));
		return addYears(calendar, n, format);
	}

	/**
	 * 获取和指定时间差n天的时间, 格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return dateStr+n年的时间
	 */
	public static String getBeforeOrNextYear(String dateStr, int n) {
		return getBeforeOrNextYear(dateStr, n, YMDHMS);
	}

	/**
	 * 获取和当前时间差n天的时间
	 * 
	 * @param n
	 * @return now+n天的时间（n=1为明天，n=-1为昨天）
	 */
	public static String getBeforeOrNextDay(int n, String format) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, n);
		strSysTime = new SimpleDateFormat(format).format(calendar.getTime());
		return strSysTime;
	}

	/**
	 * 获取和当前时间差n天的时间(date型)
	 * 
	 * @param n
	 * @return now+n天的时间（n=1为明天，n=-1为昨天）
	 */
	public static Date getBeforeOrNextDate(int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, n);
		return calendar.getTime();
	}
	
	public static Date getBeforeOrNextDate(Date date,int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, n);
		return calendar.getTime();
	}	
	
	public static Date getBeforeOrNextHour(Date date,int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, n);
		return calendar.getTime();
	}
	public static Date getBeforeOrNextSecond(Date date,int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.SECOND, n);
		return calendar.getTime();
	}
	/**
	 * 获取和当前时间差n秒的时间(date型)
	 * 
	 * @param n
	 * @return now+n秒的时间（n=1为后一秒，n=-1为前一秒）
	 */
	public static Date getDateBeforeOrNextSecond(int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, n);
		return calendar.getTime();
	}

	/**
	 * 获取和当前时间差n小时的时间
	 * 
	 * @param n
	 * @return now+n小时的时间（n=1为一小时后，n=-1为一小时前）
	 */
	public static String getBeforeOrNextHour(int n, String format) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, n);
		strSysTime = new SimpleDateFormat(format).format(calendar.getTime());
		return strSysTime;
	}

	/**
	 * 获取和指定时间差n小时的时间, 格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return dateStr+n天的时间
	 */
	public static String getBeforeOrNextHour(String dateStr, int n) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseYMDHMSDate(dateStr));
		calendar.add(Calendar.HOUR_OF_DAY, n);
		strSysTime = new SimpleDateFormat(YMDHMS).format(calendar.getTime());
		return strSysTime;
	}

	/**
	 * 获取和当前时间差n小时的时间，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return now+n小时的时间（n=1为一小时后，n=-1为一小时前）
	 */
	public static String getBeforeOrNextHour(int n) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, n);
		// Calendar.m
		strSysTime = new SimpleDateFormat(YMDHMS).format(calendar.getTime());
		return strSysTime;
	}
	/**
	 * 获取时间戳
	 * @param n 和当前时间的差距s
	 * @return
	 */
	public static Long getTimeBySecond(int n) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, n);
		// Calendar.m
		return calendar.getTime().getTime();
	}
	public static String getBeforeOrNextHour(String dateStr, int n, String format) {
		if (DataUtil.Stringg.isNullOrEmpty(dateStr)) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDate(dateStr, format));
		return addHours(calendar, n, format);
	}

	/**
	 * 获取和当前时间差n分钟的时间，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return now+n分钟的时间（n=1为一分钟后，n=-1为一分钟前）
	 */
	public static String getBeforeOrNextMunite(int n) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, n);
		// Calendar.m
		strSysTime = new SimpleDateFormat(YMDHMS).format(calendar.getTime());
		return strSysTime;
	}
	/**
	 * @param n
	 * @param format
	 * @return
	 */
	public static String getBeforeOrNextMunite(int n,String format) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, n);
		// Calendar.m
		strSysTime = new SimpleDateFormat(format).format(calendar.getTime());
		return strSysTime;
	}
	/**
	 * 获取和当前时间差n秒的时间，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param n
	 * @return now+n秒的时间（n=1为一秒后，n=-1为一秒前）
	 */
	public static String getBeforeOrNextSecond(int n) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, n);
		// Calendar.m
		strSysTime = new SimpleDateFormat(YMDHMS).format(calendar.getTime());
		return strSysTime;
	}
	/**
	 * @param n
	 * @param format
	 * @return
	 */
	public static String getBeforeOrNextSecond(int n,String format) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, n);
		// Calendar.m
		strSysTime = new SimpleDateFormat(format).format(calendar.getTime());
		return strSysTime;
	}
	/**
	 * 获取和当前时间相差的时间，并以相应的形式返回 Ex，Mongo需要的时间格式:"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
	 * 
	 * @param day
	 *            相差日
	 * @param hour
	 *            相差时
	 * @param minutes
	 *            相差分
	 * @param format
	 *            日期格式
	 * @return 符合格式的日期String
	 */
	public static String getDateWithFormat(int day, int hour, int minutes, String format) {
		String strSysTime = "";
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, day);
		calendar.add(Calendar.HOUR_OF_DAY, hour);
		calendar.add(Calendar.MINUTE, minutes);
		// Calendar.m
		strSysTime = new SimpleDateFormat(format).format(calendar.getTime());
		return strSysTime;
	}

	/**
	 * Mongo数据库需要的实际格式
	 * 
	 * @see com.ymttest.utils.YMTDateUtil#getDateWithFormat(int, int, int,
	 *      String)
	 * @return String yyyy-MM-dd'T'HH:mm:ss.SSS'Z
	 */
	public static String getDateForMongoFormat(int day, int hour, int minutes) {
		return getDateWithFormat(day, hour, minutes, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
	}
	
	/**
	 * 获取时间差
	 * @param date
	 * @param date2
	 * @return 时间差，date-date2，单位年（大约）
	 */
	public static BigDecimal diffYear(Date date, Date date2) {
		
		String diffDays = String.valueOf(YMTDateUtil.diffDay(date, date2));
		
		return new BigDecimal(diffDays).divide(new BigDecimal("365"), 2, BigDecimal.ROUND_DOWN);
	}

	/**
	 * 获取时间差
	 * 
	 * @param date
	 * @param date2
	 * @return 时间差， date-date2,单位天
	 */
	public static long diffDay(Date date, Date date2) {
		return Long.valueOf((date.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24));
	}

	/**
	 * 获取时间差
	 * 
	 * @param date
	 * @param date2
	 * @return 时间差， date-date2,单位小时
	 */
	public static long diffHour(Date date, Date date2) {
		return Long.valueOf((date.getTime() - date2.getTime()) / (1000 * 60 * 60));
	}

	/**
	 * 获取时间差
	 * 
	 * @param date
	 * @param date2
	 * @return 时间差， date-date2,单位分
	 */
	public static int diffMinute(Date date, Date date2) {
		return (int) ((date.getTime() - date2.getTime()) / (1000 * 60));
	}

	/**
	 * 获取时间差
	 * 
	 * @param date
	 * @param date2
	 * @return 时间差， date-date2,单位S
	 */
	public static int diffSecond(Date date, Date date2) {
		return (int) ((date2.getTime() - date.getTime()) / 1000);
	}

	/**
	 * 获取时间差
	 * 
	 * @param long
	 * @param long2
	 * @return 时间差， date-date2,单位S
	 */
	public static int diffSecond(long date, long date2) {
		return (int) ((date2 - date) / 1000);
	}

	/**
	 * 获取时间差
	 * 
	 * @param date
	 * @param date2
	 * @return 时间差， date-date2,单位ms
	 */
	public static int diffMs(Date date, Date date2) {
		return (int) ((date2.getTime() - date.getTime()));
	}

	/**
	 * 当前时间是否在9点到21点
	 * 
	 * @param date
	 * @return boolean
	 */
	public static boolean isBetweenDate(Date date) {
		String begin = getSysTime("yyyy-MM-dd") + " 09:00:00";
		String end = getSysTime("yyyy-MM-dd") + " 21:00:00";
		boolean result = false;
		try {
			if (date.before(new SimpleDateFormat(YMDHMS).parse(end))
							&& date.after(new SimpleDateFormat(YMDHMS).parse(begin))) {
				result = true;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 传入指定格式的时间，然后转化成另一种格式的时间
	 * 
	 * @param datestr
	 *            2015-12-13
	 * @param strPattern
	 *            yyyy-MM-dd
	 * @param newFormat
	 *            yyyy-MM-dd'T'HH:mm:ss.SSS'Z
	 * @return String newFormat
	 * @throws ParseException
	 */
	public static String convertDateStrToAnotherFormat(String datestr, String strPattern, String newFormat)
					throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(strPattern);
		Date date = sdf.parse(datestr);
		return getFormatTime(date, newFormat);
	}

	/**
	 * 转化时间类型为java中Date
	 *
	 * @author chenjiazhu
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateFromMongo(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		return sdf.parse(date.substring(7, date.length() - 1));
	}

	public static Date getDate(String date, String format) {
		Date d = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			d =  sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}
	/**
	 * 用于mongo获取时间
	 * YMTDateUtil.getDate(pdb.get("end").toString(), YMTDateUtil.YMDGMT,Locale.US),
	 * @param date
	 * @param format
	 * @param Locale
	 * @return
	 */
	public static Date getDate(String date, String format, Locale Locale) {
		Date d = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format,Locale);
			d =  sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}
	/**
	 * 转化返回中的时间类型为java中Date
	 *
	 * @author chenjiazhu
	 * @param date
	 *            ,格式："2016-05-26T23:59:00"
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateFromReturn(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		return sdf.parse(date.substring(0, 18));
	}

	/**
	 * 把传入Date转换成2015-12-11T23:59:00格式
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getFormatDateString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		return sdf.format(date);
	}
	/**
	 *  把传入Date转换成format格式
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getFormatDateString(Date date,String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	/**
	 * 将接口返回的格式为"2016-07-11T18:55:37.913"转换为yyyy-MM-dd HH:mm:ss.SSS的格式
	 * 
	 * @author songyefeng
	 * @param respDate
	 * @return
	 */
	public static String parseRespDate(Object respDate) {
		String s = "null";
		if (null != respDate && !"null".equals(respDate)) {
			s = String.valueOf(respDate);
			s = s.replace("T", " ");
			if (s.contains(".") == false) {
				s = s + ".0";
			}
		}
		return s;
	}

	/**
	 * 将接口返回的格式为"2016-07-11T18:55:37"转换为yyyy-MM-dd HH:mm:ss的格式
	 * 
	 * @author songyefeng
	 * @param respDate
	 * @return
	 */
	public static String parseRespDate2(Object respDate) {
		String s = "null";
		if (null != respDate && !"null".equals(respDate)) {
			s = String.valueOf(respDate);
			if (s.contains(".")) {
				s = s.split("\\.")[0];
			}
			s = s.replace("T", " ");
		}
		return s;
	}
	
	/**
     * 判断time是否在from，to之内
     *
     * @param time 指定日期
     * @param from 开始日期
     * @param to   结束日期
     * @return
     */
	
	public static boolean belongCalendar(Date time, Date from, Date to) {
        Calendar date = Calendar.getInstance();
        date.setTime(time);

        Calendar after = Calendar.getInstance();
        after.setTime(from);

        Calendar before = Calendar.getInstance();
        before.setTime(to);

        if (date.after(after) && date.before(before)) {
            return true;
        } else {
            return false;
        }
    }

	/**
	 * (yyyy-MM-dd HH:mm:ss)日期格式字符串 转 时间戳(秒)
	 * 
	 * @param dateStr
	 *            日期格式字符串
	 * @return
	 */
	public static Long date2TimeStamp(String dateStr) {
		Timestamp ts = Timestamp.valueOf(dateStr);
		return ts.getTime() / 1000;
	}

	/**
	 * (EEE MMM dd HH:mm:ss zzz yyyy)CST日期格式字符串 转 时间戳(秒)
	 * 
	 * @param dateStr
	 *            日期格式字符串
	 * @return
	 */
	public static Long CST2TimeStamp(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.UK);

		Date date = null;
		try {
			date = (Date) sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return date.getTime();
	}

	/**
	 * (EEE MMM dd HH:mm:ss zzz yyyy)CST日期格式字符串 转 yyyy-MM-dd'T'HH:mm:ss.000XXX
	 * 
	 * @param dateStr
	 *            日期格式字符串
	 * @return
	 */
	public static String CST2ShoppingcartTime(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

		Date date = null;
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

		try {
			date = (Date) sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return sdf1.format(date);
	}

	public static void waitTime(double sec) {
		try {
			Thread.sleep((long) (sec * 1000));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	/**
	 * 获得当前日期的年份/月份/日期
	 * 
	 * @param type(year, month, date)
	 *           	返回类型（当前的年或月或日）
	 * @return
	 */
	public static int getCurrentYearMonthDate(String type) {

		Calendar a = Calendar.getInstance();
		
		if (type.equalsIgnoreCase("year"))
			return a.get(Calendar.YEAR);
		else if (type.equals("month"))
			return a.get(Calendar.MONTH) + 1;
		else if (type.equals("date"))
			return a.get(Calendar.DATE);
		else
			return 0;
	}

	public static void main(String[] args) throws Exception {
		// System.out.print(convertDateStrToAnotherFormat("2015-12-13",
		// "yyyy-MM-dd",
		// "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
		// System.out.println(getFormatDateString(new Date()));
		// System.out.print(convertDateStrToAnotherFormat("2016-05-05T11:35:32.870Z",
		// "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd HH:mm:ss.SSS"));

		// Long s =
		// Long.valueOf((parseYMDHMSDate(getBeforeOrNextDay(3)).getTime() -
		// parseYMDHMSDate(getBeforeOrNextDay(1)).getTime())
		// / (1000 * 60 * 60 * 24));
		// System.out.println(diffHour(parseYMDHMSDate("2016-01-02 12:00:00"),
		// parseYMDHMSDate("2016-01-01 23:59:59")));
		/*System.out.println(getBeforeOrNextDay(1));
		System.out.println(addDays(-1, YMD));
		System.out.println(getBeforeOrNextDay("2016-01-02 13:14:16", 1, YMD));
		System.out.println(getBeforeOrNextMonth(2));
		System.out
				.println(getBeforeOrNextMonth("2016-01-02 13:14:16", -3, YMD));
		System.out.println(getBeforeOrNextMonth("2016-01-02 13:14:16", 4));
		System.out.println(getBeforeOrNextYear(-1));
		System.out.println(getBeforeOrNextYear("2016-01-02 13:14:16.123", -2,
				YMDHMSS));
		System.out.println(getBeforeOrNextYear("2016-01-02 13:14:16", 2));
		System.out.println(parseDate("2015-10-13 19:48:07.01", YMDHMSS));
		System.out.println(parseDate("2016-01-02 13:14:16.122", YMDHMSS)
				.compareTo(parseDate("2016-01-02 13:14:16.123", YMDHMSS)));
		System.out.println(parseDate("2016-01-02 13:14:16.124", YMDHMSS).after(
				parseDate("2016-01-02 13:14:16.123", YMDHMSS)));*/
		// System.out.println(getBeforeOrNextHour("2016-01-02 12:00:00", 2));
		// System.out.println(parseYMDHMSDate(
		// getBeforeOrNextHour("2016-08-17 12:00:00", 2)).getTime()
		// - System.currentTimeMillis());
		//LiveProductModelWapper liveProductModelWapper = new LiveProductModelWapper();
		//Map<String, Object> map = liveProductModelWapper.getByProductId("28b20646-3218-4936-949f-37b1205c6e59");
		
		//String ValidStart = map.get("EndTime").toString();
//		String ValidStart = "Thu Dec 22 22:00:00 CST 2016";
//		//Thu Dec 22 22:00:00 CST 2016
//		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
//		
//		Date date = (Date) sdf.parse(ValidStart);
//		//"yyyy-MM-dd'T'HH:mm:ss.SSS Z"
//		
//		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000XXX");
//		System.out.println(sdf1.format(date));
//		String formatStr2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
//
//		System.out.println(String.valueOf("1482128676057").substring(0, 10).concat("000"));
//		
//		System.out.println(diffSecond(new Date(), date));
//		System.out.println(getBeforeOrNextMonth(-1, YMDTHMS_STRING));
//		System.out.println(getBeforeOrNextDay("2016-12-19T11:50:28.123", 0, YMDTHMS_STRING));
//		System.out.println(getBeforeOrNextDay("2016-12-19T11:50:28", 0, YMDTHMS_STRING));
//		System.out.println(getBeforeOrNextMonth(-6));
//		System.out.println(getBeforeOrNextHour("2016-12-19T11:50:28.123", 1, YMDTHMSS_STRING));
//		Date d1 = parseDate(getBeforeOrNextHour("2016-12-19T11:50:28.123", 1, YMDTHMSS_STRING), YMDTHMSS_STRING);
//		System.out.println(d1);
//		System.out.println(getFormatTime(d1, YMDTHMSS_STRING));
		String string="text/html";
		System.out.println(string.substring(string.indexOf("/")+1,string.length()));
		
		System.out.println(YMTDateUtil.getBeforeOrNextMinutes(
				getSysTime(YMTDateUtil.YMDTHMS_STRING), 30,
				YMTDateUtil.YMDTHMS_STRING));
		
		System.out.println(YMTDateUtil.getBeforeOrNextHour(30, YMTDateUtil.YMDTHMS_STRING));
	}
	
//	public static void main(String[] args) {
//		String str="{ FINGERPRINT=: generic/vbox86p/vbox86p:5.0/LRX21M/buildbot02241201:userdebug/test-keys,  USER=: buildbot,  BRAND=: generic,  BOARD=: unknown, Product=: vbox86p,  MANUFACTURER=: Genymotion,  TAGS=: test-keys,  DEVICE=: vbox86p,  MODEL=: Google Nexus 4 - 5.0.0 - API 21 - 768x1280,  DISPLAY=: vbox86p-userdebug 5.0 LRX21M eng.buildbot.20150224.115852 test-keys,  CPU_ABI=: x86,  SDK=: 21,  VERSION.RELEASE=: 5.0,  ID=: LRX21M,  VERSION_CODES.BASE=: 1, Resolution=:768x1280}";
//		System.out.print(str.toLowerCase());
//	}
}
